<template>
  <el-dialog
    :title="$t('auth.role.relatepermissions')"
    :visible.sync="dialogPermissionVisible"
    width="600px"
  >
    <div class="borde-style">
      <!--<div style="overflow:auto;height:310px;border:1px solid;border-width: 1px;border-color:#eeeeee">-->
      <el-tree
        :data="treeData"
        show-checkbox
        node-key="id"
        ref="tree"
        :props="treeProps"
        default-expand-all
        @check="treeCheck"
        top="20"
      ></el-tree>
    </div>
    <template
      slot="footer"
      class="dialog-footer"
    >
      <center>
        <el-button @click="dialogPermissionVisible=false">{{$t('auth.common.cancel')}}</el-button>
        <el-button
          type="primary"
          @click="setData()"
        >{{$t('auth.common.confirm')}}</el-button>
      </center>
    </template>
  </el-dialog>
</template>

<script>
/* eslint-disable */
import { getRolePermissions, SetRolePermisssions } from "../../../api/relate";
export default {
  data () {
    return {
      treeProps: {
        label: 'description',
        children: 'children'
      },
      listLoading: false, //数据加载等待动画
      dialogPermissionVisible: false,
      tempRole: {
        name: "",
        id: "",
        description: ""
      }
    };
  },
  name: "permissions",
  props: ["treeData"],
  methods: {
    onPermissionsClick (role) {
      this.tempRole.name = role.name;
      this.tempRole.id = role.id;
      this.tempRole.description = role.description;
      this.dialogPermissionVisible = true;
      this.InitTreeState(role.id);
    },
    InitTreeState (id) {
      getRolePermissions(id).then(body => {
        if (body.data.code === '401') { // 判断用户是否在其他地方被踢出
          window.location.reload()
          return
        }
        if (body.data.success !== true) {
          console.log("get role-permissions relate error");
          return;
        }
        let permissionList = [];
        for (let data of body.data.data) {
          permissionList.push(data.id);
        }
        if (body.data.data && body.data.data.length === 1 && body.data.data[0].permission_code === '*') {
          permissionList = [1]
        } 
        this.$refs.tree.setCheckedKeys(permissionList);
        let info = {};
        info.checkedKeys = this.$refs.tree.getCheckedKeys();
        info.halfCheckedKeys = this.$refs.tree.getHalfCheckedKeys();
        this.treeCheck(this.treeData, info);
      });
    },
    treeCheck (data, checkInfo) {
      const funcSetCheck = node => {
        if (
          checkInfo.checkedKeys.find(id => id === node.id) !==
          undefined
        ) {
          node.checked = 1;
        } else if (
          checkInfo.halfCheckedKeys.find(id => id === node.id) !==
          undefined
        ) {
          node.checked = 2;
        } else node.checked = 0;
      };
      for (let curNode of this.treeData) {
        for (let node of curNode.g_travel()) {
          funcSetCheck(node);
        }
      }
    },
    setData () {
      const travelTree = (treeNode, list) => {
        if (treeNode.checked === 1) {
          list.push(treeNode.id);
        } else {
          for (let child of treeNode.children) {
            travelTree(child, list);
          }
        }
      };
      let checkList = [];
      for (let node of this.treeData) {
        travelTree(node, checkList);
      }
      if (checkList && checkList.length === 1 && checkList[0] === 1) {
        let allPermission = this.$parent.allPermission
        let permission = allPermission.filter(p => p.permission_code === '*')[0]
        let permissionId = permission.id
        checkList = [permissionId]
      }
      SetRolePermisssions(this.tempRole.id, checkList).then(data => {
        if (data.data.code === '401') { // 判断用户是否在其他地方被踢出
          window.location.reload()
          return
        }
        if (data.data.success !== true) {
          this.$message.error(data.data.message);
          console.log(data.data.message);
          return;
        }
        let msg = this.$t('auth.common.relatesucess');
        this.dialogPermissionVisible = false;
        //this.$refs[formData].resetFields(); //表单重置
        this.$message({
          message: msg,
          type: "success",
          duration: 1 * 1000,
          onClose: () => {
            this.$parent.getList();
          }
        });
      });
    }
  }
};
</script>


<style lang="scss" rel="stylesheet/scss" scoped>
</style>
